// pages/group_intro/group_info.js
var app = getApp();

var common = require("../../common/common.js").common;

var WxParse = require("../../wxParse/wxParse.js");

var {$author: $author} = require("../../components/base/authorization.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        group_info: "",
        leader_id: "",
        showInfoTip: false,
        //授权失败的弹框
        is_leader: 0,
        is_au: 0,
        options: "",
        s_leader: [],
        rec_leader_id: "",
        modalHide: true
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        //分享来的团长
                var rec_leader_id;
        //解析分享二维码和分享首页的数据scene
                if (options) {
            var scene = decodeURIComponent(options.scene);
            var s_leader = scene.split(":");
            that.setData({
                options: options,
                s_leader: s_leader
            });
        }
        //这边要改成解析链接过来的
                if (options.my_leaderID) {
            rec_leader_id = options.my_leaderID;
        } else {
            rec_leader_id = s_leader[1];
        }
        that.setData({
            rec_leader_id: rec_leader_id
        });
        if (that.data.rec_leader_id) {
            wx.setStorageSync("rec_leader_id", that.data.rec_leader_id);
        } else {
            wx.setStorageSync("rec_leader_id", "");
        }
        //首次直接进入，拿openid
                if (app.globalData.user) {
            //如果直接拿到openid
            that.requestStart(app.globalData.user.openid);
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.requestStart(res.openid);
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //请求开始
    requestStart: function(openid) {
        this.getBase();
        this.getData();
        this.send_leader();
        this.is_leader();
    },
    //获取申请团长简介
    getData: function() {
        var that = this;
        common.request({
            endpoint: "rich/leader",
            method: "post",
            header: {
                "content-type": "application/json"
            },
            success: res => {
                if (res.extra.rich) {
                    var group_info = res.extra.rich;
                    that.setData({
                        group_info: WxParse.wxParse("group_info", "html", group_info, that, 0)
                    });
                }
            },
            fail: res => {}
        });
    },
    //基础信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.leader_title ? res.extra.leader_title + "申请" : "团长申请"
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2
                });
            }
        });
    },
    send_leader: function() {
        var that = this;
        common.request({
            endpoint: "member/recommend_leader",
            method: "post",
            data: {
                leader_id: that.data.rec_leader_id
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {},
            fail: res => {}
        });
    },
    //是否为团长
    is_leader: function() {
        var that = this;
        common.request({
            endpoint: "leader/is_leader",
            method: "post",
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    is_leader: res.extra.is_leader,
                    is_au: res.extra.is_au
                });
            },
            fail: res => {}
        });
    },
    //*******************************************************
    //
    // 整理合并产品页所有请求 hgj 2019.11.04 
    // 下面为封装的共用方法调用
    //
    //*******************************************************
    //首页弹窗
    getUserInfo: function() {
        common.getUserInfo();
    },
    navTo: function() {
        try {
            var value = wx.getStorageSync("user");
            if (value.nickname) {
                // Do something with return value
            } else {
                wx.showModal({
                    title: "提示",
                    content: "请用户授权登录之后再进行此操作"
                });
            }
        } catch (e) {
            // Do something when catch error
        }
        var that = this;
        var is_leader = that.data.is_leader;
        var is_au = that.data.is_au;
        common.getUserInfoNew({
            success() {
                if (is_leader == 0 && is_au == 0) {
                    //区域团长分享
                    if (that.data.options.leader_area_id) {
                        wx.navigateTo({
                            url: "/pages/group/group?leader_area_id=" + that.data.options.leader_area_id
                        });
                    } else {
                        wx.navigateTo({
                            url: "/pages/group/group"
                        });
                    }
                } else {
                    if (is_leader == 1) {
                        wx.showModal({
                            title: "提示",
                            content: "你已经是" + that.data.leaderName,
                            confirmText: "跳转首页",
                            success(res) {
                                if (res.confirm) {
                                    wx.switchTab({
                                        url: "/pages/index/index"
                                    });
                                }
                            }
                        });
                    } else {
                        wx.showModal({
                            title: "提示",
                            content: "你的" + that.data.leaderName + "申请正在审核",
                            confirmText: "跳转首页",
                            success(res) {
                                if (res.confirm) {
                                    wx.switchTab({
                                        url: "/pages/index/index"
                                    });
                                }
                            }
                        });
                    }
                }
            }
        });
    }
});